package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityDetailBinding;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class DetailActivity extends BaseAc<ActivityDetailBinding> {

    public static StkResBean bean;

    @Override
    protected int onCreate() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivDetailBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivDetailShare.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDetailShare://分享
                IntentUtil.shareText(mContext, bean.getUrl());
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvDetailTitle.setText(bean.getName());
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            Date date = format.parse(bean.getCreateAt());
            String time = new SimpleDateFormat("yyyy年MM月dd日").format(date);
            mDataBinding.tvDetailDate.setText(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        mDataBinding.webView.getSettings().setJavaScriptEnabled(true);
        // 注入JavaScript函数来改变文字颜色
        String jsCode = "javascript:document.body.style.setProperty('color', 'rgba(255, 226, 194,0.7)');";
        // 在WebView加载完成后注入JavaScript代码
        mDataBinding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mDataBinding.webView.evaluateJavascript(jsCode, null);
            }
        });
        mDataBinding.webView.setBackgroundColor(Color.TRANSPARENT);
        mDataBinding.webView.loadUrl(bean.getUrl());
    }
}